package com.sxpi.chapter.service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sxpi.chapter.mapper.UserMapper;
import com.sxpi.chapter.pojo.User;
import com.sxpi.chapter.service.vip.IUserVipService;
import com.sxpi.chapter.service.vip.NormalUserServiceImpl;
import com.sxpi.chapter.service.vip.SVipUserServiceImpl;
import com.sxpi.chapter.service.vip.VipUserServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @Author: 董伟豪
 * @Date: 2022/12/30 16:47
 */
@Service
@Slf4j
public class UserServiceImpl extends ServiceImpl<UserMapper,User> implements IUserService{

    /**
     * 根据id查询用户
     * @param id
     * @return
     */
    @Override
    public User getUser(Long id) {
        return this.getById(id);
    }

    @Override
    public String buyCourse(Long id, Long courseId) {
        // 充值的用户 从数据库或者缓存中获取
        User user = this.getById(id);
        if(user != null){
            // 根据用户不同的身份获取到不同的价格
            Integer vip = user.getVip();
            Double corn = 0D;
            Double money = 100D;
            if(vip.equals(1)){
                money = money * 1;
                corn = money * 0.75;
            }else if(vip.equals(2)){
                money = money * 0.75;
                corn = money * 0.55;
            }else if(vip.equals(3)){
                money = money * 0.5;
                corn = money * 0.35;
            }
            // 开始进行支付逻辑处理
            log.info("购买的课程金额是：{},学习币是{}",money,corn);
            return "success";
        }
        return "fail";
    }

    @Autowired
    private IUserVipService userVipService;

    @Override
    public String buyCourseDesign(Long id, Long courseId) {
        // 充值的用户 从数据库或者缓存中获取
        User user = this.getById(id);
        if(user != null){
            // 根据用户不同的身份获取到不同的价格
            Integer vip = user.getVip();
            Double corn = 0D;
            Double money = 100D;


            IUserVipService vipService = UserFactoryBean.getInstance(vip);


            money = vipService.getMoney(money);
            corn = vipService.getCorn(corn);
            // 开始进行支付逻辑处理
            log.info("购买的课程金额是：{},学习币是{}",money,corn);
            return "success";
        }
        return "fail";
    }

}
